package com.ruoyi.project.pay;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.Md5Utils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.channel.businessCapital.domain.BusinessCapital;
import com.ruoyi.project.channel.businessRate.domain.BusinessRate;
import com.ruoyi.project.channel.channelOrder.domain.ChannelOrder;
import com.ruoyi.project.channel.payerBusinessInfo.domain.PayerBusinessInfo;
import com.ruoyi.project.channel.payerBusinessInfo.service.IPayerBusinessInfoService;
import com.ruoyi.project.channel.payerInfo.domain.PayerInfo;
import com.ruoyi.project.channel.payerInfo.service.IPayerInfoService;
import com.ruoyi.project.monitor.server.domain.Sys;
import com.ruoyi.project.pay.facai.SignUtil;
import com.ruoyi.project.pay.qixing.AESUtil;
import com.ruoyi.project.pay.qixing.CryptoUtil;
import com.ruoyi.project.pay.qixing.StringUtil;
import com.ruoyi.project.pay.vnet.SHA256WithRSAUtils;
import com.ruoyi.project.pay.vnet.SSLView;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.system.user.service.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.HttpsURLConnection;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.security.PrivateKey;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 通道订单 服务层实现
 *
 * @author ruoyi
 * @date 2019-04-09
 */
@Service
public class ThirdPayerServiceImpl {
    @Autowired
    private IPayerInfoService payerInfoService;
    @Autowired
    private IPayerBusinessInfoService payerBusinessInfoService;
    @Autowired
    private PayConfig payConfig;
    private static final Logger log = LoggerFactory.getLogger(ThirdPayerServiceImpl.class);
    @Autowired
    private IUserService userService;

    private String parseNullString(String value) {
        if (value == null) {
            return "";
        }
        return value;
    }

    /**
     * 调用下游支付商创建订单接口 支付商如果调用成功 必须返回支付uri地址
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
//        String url = payConfig.getCaiNiaoHost() + "/pay/bizOrder";
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNextOrder:找不到支付商");
            return null;
        }

        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNextOrder:找到多个支付商商户");
            return null;
        }
        User user = userService.selectUserById(channelOrder.getBusinessId());

        String payerBusinessId = businessRate.getPayerBusinessId();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("businessId=").append(payerBusinessId);
        stringBuilder.append("&payType=").append(channelOrder.getPayType());
        stringBuilder.append("&childOrderno=").append(channelOrder.getPtOrderno());
        stringBuilder.append("&amount=").append(channelOrder.getAmount());
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        stringBuilder.append("&time=").append(time);
        stringBuilder.append("&").append(payerBusinessInfos.get(0).getApiKey());
        String sign = "";
        try {
            log.info("setToNextOrderString:" + stringBuilder.toString());
            sign = Md5Utils.androidMD5(stringBuilder.toString());
            System.out.println("setToNextOrderStringSign:" + sign);
        } catch (Exception e) {
        }
        RestTemplate restTemplate = new RestTemplate();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("businessId", payerBusinessId);
        jsonObject.put("payType", channelOrder.getPayType());
        jsonObject.put("childOrderno", channelOrder.getPtOrderno());
        jsonObject.put("amount", channelOrder.getAmount());
        jsonObject.put("time", time);
        jsonObject.put("sign", sign);
        String callbackUrl = payConfig.getPayCenterHost() + "/admin/pay/order/patch";
        jsonObject.put("serverUrl", callbackUrl);
        jsonObject.put("returnUrl", callbackUrl);
        jsonObject.put("payerUserName", user.getLoginName());
        jsonObject.put("fromAlipayAccount", channelOrder.getFromAlipayAccount()); // usdt地址
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> entity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
        JSONObject body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, JSONObject.class).getBody();
        log.info("setToNextOrder:" + body.toJSONString());
        return body;
    }

    /**
     * 调用下游支付商代付接口
     *
     * @param businessCapital
     * @return
     */
    public JSONObject setToNextWithdraw(BusinessCapital businessCapital, BusinessRate businessRate) {
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNextOrder:找不到支付商");
            return null;
        }

        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNextOrder:找到多个支付商商户");
            return null;
        }
        String payerBusinessId = businessRate.getPayerBusinessId();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("businessId=").append(payerBusinessId);
        stringBuilder.append("&payType=107");
        stringBuilder.append("&childOrderno=").append(businessCapital.getCapitalId());
        stringBuilder.append("&amount=").append(businessCapital.getRealAmount());
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        stringBuilder.append("&time=").append(time);
        stringBuilder.append("&bankNo=").append(businessCapital.getBankNo());
        String sign = "";
        try {
            log.info("setToNextWithdraw:" + stringBuilder.toString());
            sign = Md5Utils.androidMD5(stringBuilder.toString()+"&"+payerBusinessInfos.get(0).getApiKey());
            System.out.println("setToNextWithdraw:" + sign);
        } catch (Exception e) {
        }
        stringBuilder.append("&sign=").append(sign);
        stringBuilder.append("&bankName=").append(businessCapital.getBankName());
        stringBuilder.append("&userName=").append(businessCapital.getUserName());

        String callbackUrl = payConfig.getPayCenterHost() + "/callback/withdraw";
        stringBuilder.append("&serverUrl=").append(callbackUrl);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<String> entity = new HttpEntity<String>(stringBuilder.toString(), headers);
        JSONObject body = restTemplate.exchange(payerInfo.getRemark(), HttpMethod.POST, entity, JSONObject.class).getBody();
        log.info("setToNextOrder:" + body.toJSONString());
        return body;
    }

    /**
     * 知宇类型的通用接口
     *
     * @param channelOrder
     * @return
     */
    private JSONObject setToZhiYuNextOrder(ChannelOrder channelOrder, BusinessRate businessRate, String path) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNextOrder:找到多个支付商商户");
            return null;
        }
        String payerBusinessId = businessRate.getPayerBusinessId();
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/" + path;
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String stringSignTemp = "pay_amount=" + channelOrder.getAmount() + "&pay_applydate=" + time + "&pay_bankcode=" + businessRate.getPayerPayType() + "&pay_callbackurl=" + callbackUrl + "&pay_memberid=" + payerBusinessId + "&pay_notifyurl=" + callbackUrl + "&pay_orderid=" + channelOrder.getPtOrderno() + "&key=" + payerBusinessInfos.get(0).getApiKey();
        String sign = "";
        try {
            log.info("setToPddNextOrderString:" + stringSignTemp);
            sign = Md5Utils.androidMD5(stringSignTemp).toUpperCase();
            log.info("setToPddNextOrderString加密结果:" + sign);
        } catch (Exception e) {
        }
        RestTemplate restTemplate = new RestTemplate();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("pay_memberid=").append(payerBusinessId);
        stringBuilder.append("&pay_orderid=").append(channelOrder.getPtOrderno());
        stringBuilder.append("&pay_applydate=").append(time);
        stringBuilder.append("&pay_bankcode=").append(businessRate.getPayerPayType());
        stringBuilder.append("&pay_notifyurl=").append(callbackUrl);
        stringBuilder.append("&pay_callbackurl=").append(callbackUrl);
        stringBuilder.append("&pay_amount=").append(channelOrder.getAmount());
        stringBuilder.append("&pay_md5sign=").append(sign);
        stringBuilder.append("&pay_productname=收款");
        stringBuilder.append("&pay_productnum=");
        stringBuilder.append("&pay_productdesc=");
        stringBuilder.append("&pay_producturl=");

        log.info("setToPDDNextOrder" + stringBuilder.toString());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity entity = new HttpEntity(stringBuilder.toString(), headers);
        String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
        log.info("setToPDDNextOrder result:" + body);
        JSONObject object = JSONObject.parseObject(body);
        String _payUrl = "";
        if ("qx2".equals(path)) {
            _payUrl = object.getString("pay_url");
        } else {
            JSONObject data = object.getJSONObject("data");
            _payUrl = data.getString("qrCode");
        }
        JSONObject result = new JSONObject();
        JSONObject payData = new JSONObject();
        payData.put("payUrl", _payUrl);
        result.put("data", payData);
        return result;
    }

    /**
     * 拼多多
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToPDDNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        return setToZhiYuNextOrder(channelOrder, businessRate, "pdd");
    }

    /**
     * 七星
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToQx2NextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        return setToZhiYuNextOrder(channelOrder, businessRate, "qx2");
    }

    /**
     * 聚合支付
     *
     * @param channelOrder
     * @param businessRate
     * @return
     */
    public JSONObject setToJHNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToJHNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToJHNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToJHNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/jh";
        String stringSignTemp = "appid=" + businessRate.getPayerBusinessId() + "&code=" + businessRate.getPayerPayType() + "&money=" + channelOrder.getAmount() + "&nonce_str="
                + System.currentTimeMillis() + "&noticeurl=" + callbackUrl +
                "&orderid=" + channelOrder.getPtOrderno() + "&service=pay&time=" + System.currentTimeMillis() + "&version=2.1";
        String sign = "";
        try {
            log.info("setToJHNextOrder:" + stringSignTemp + "&key=" + payerBusinessInfos.get(0).getApiKey());
            sign = Md5Utils.androidMD5(stringSignTemp + "&key=" + payerBusinessInfos.get(0).getApiKey());
            log.info("setToJHNextOrder加密结果:" + sign);
        } catch (Exception e) {
        }
        RestTemplate restTemplate = new RestTemplate();
        stringSignTemp = stringSignTemp + "&sign=" + sign;
        log.info("setToPDDNextOrder" + stringSignTemp);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity entity = new HttpEntity(stringSignTemp, headers);
        String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
        log.info("setToJHNextOrder result:" + body);
        JSONObject object = JSONObject.parseObject(body);
        int code = object.getIntValue("code");
        if (code != 0) {
            return null;
        }
        JSONObject data = object.getJSONObject("data");
        String _payUrl = data.getString("payurl");
        JSONObject result = new JSONObject();
        JSONObject payData = new JSONObject();
        payData.put("payUrl", _payUrl);
        result.put("data", payData);
        return result;
    }

    /**
     * 调用下游支付商创建订单接口 支付商如果调用成功 必须返回支付uri地址
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToYHBNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/yhb";
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String stringSignTemp = "pay_amount=" + channelOrder.getAmount() + "&pay_applydate=" + time
                + "&pay_bankcode=" + businessRate.getPayerPayType() + "&pay_callbackurl=" + callbackUrl + "&pay_memberid=" + businessRate.getPayerBusinessId()
                + "&pay_notifyurl=" + callbackUrl + "&pay_orderid=" + channelOrder.getPtOrderno() + "&key=" + payerBusinessInfos.get(0).getApiKey();
        String sign = "";
        try {
            log.info("setToYHBNextOrderSign:" + stringSignTemp);
            sign = Md5Utils.androidMD5(stringSignTemp).toUpperCase();
            log.info("setToPddNextOrderString加密结果:" + sign);
        } catch (Exception e) {
        }
        RestTemplate restTemplate = new RestTemplate();
        stringSignTemp = stringSignTemp + "&pay_md5sign=" + sign + "&pay_returntype=2";
        log.info("setToPDDNextOrder" + stringSignTemp);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity entity = new HttpEntity(stringSignTemp, headers);
        String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
        log.info("setToPDDNextOrder result:" + body);
        JSONObject object = JSONObject.parseObject(body);
        String status = object.getString("status");
        if (!"success".equals(status)) {
            return null;
        }
        JSONObject data = object.getJSONObject("data");
        String _payUrl = data.getString("pay_url");
        JSONObject result = new JSONObject();
        JSONObject payData = new JSONObject();
        payData.put("payUrl", _payUrl);
        payData.put("third", true);
        result.put("data", payData);
        return result;
    }

    /**
     * 森火
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToSHNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/yhb";
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String stringSignTemp = "pay_amount=" + channelOrder.getAmount() + "&pay_applydate=" + time
                + "&pay_bankcode=" + businessRate.getPayerPayType() + "&pay_callbackurl=" + callbackUrl + "&pay_memberid=" + businessRate.getPayerBusinessId()
                + "&pay_notifyurl=" + callbackUrl + "&pay_orderid=" + channelOrder.getPtOrderno() + "&key=" + payerBusinessInfos.get(0).getApiKey();
        String sign = "";
        try {
            log.info("setToYHBNextOrderSign:" + stringSignTemp);
            sign = Md5Utils.androidMD5(stringSignTemp).toUpperCase();
            log.info("setToPddNextOrderString加密结果:" + sign);
        } catch (Exception e) {
        }
        RestTemplate restTemplate = new RestTemplate();
        stringSignTemp = stringSignTemp + "&pay_md5sign=" + sign + "&pay_returntype=2";
        log.info("setToPDDNextOrder" + stringSignTemp);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity entity = new HttpEntity(stringSignTemp, headers);
        String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
        log.info("setToPDDNextOrder result:" + body);
        JSONObject object = JSONObject.parseObject(body);
        String status = object.getString("status");
        if (!"success".equals(status)) {
            return null;
        }
        JSONObject data = object.getJSONObject("data");
        String _payUrl = data.getString("pay_url");
        JSONObject result = new JSONObject();
        JSONObject payData = new JSONObject();
        payData.put("payUrl", _payUrl);
        result.put("data", payData);
        return result;
    }

    /**
     * 大圣
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToDSNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/ds/" + businessRate.getPayerBusinessId();
        String stringSignTemp = channelOrder.getAmount() + channelOrder.getPtOrderno() + businessRate.getPayerPayType() + callbackUrl
                + callbackUrl + "1";
        String sign = "";
        try {
            sign = Md5Utils.androidMD5(stringSignTemp);
            sign = Md5Utils.androidMD5(sign + payerBusinessInfos.get(0).getApiKey());
        } catch (Exception e) {
        }
        stringSignTemp = "price=" + channelOrder.getAmount() + "&out_order_id=" + channelOrder.getPtOrderno() + "&type=" + businessRate.getPayerPayType() + "&notifyurl=" + callbackUrl
                + "&returnurl=" + callbackUrl + "&extend=1" + "&app_id=" + businessRate.getPayerBusinessId() + "&sign=" + sign;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity entity = new HttpEntity(headers);
        JSONObject object = restTemplate.exchange(payerInfo.getServiceUrl() + "?" + stringSignTemp, HttpMethod.GET, entity, JSONObject.class).getBody();
        log.info("setToDSNextOrder result:" + object);
        int code = object.getIntValue("code");
        if (code != 1) {
            log.error("创建大圣订单失败");
            return object;
        }
        JSONObject data = object.getJSONObject("data");
        String _payUrl = data.getString("h5url");
        JSONObject result = new JSONObject();
        JSONObject payData = new JSONObject();
        payData.put("payUrl", _payUrl);
        result.put("data", payData);
        return result;
    }

    /**
     * 大圣
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setTo126NextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 跳过SSL 验证
        try {
            SSLView.trustAllHttpsCertificates();
        } catch (Exception error) {
            log.error(error.getMessage(), error);
        }

        HttpsURLConnection.setDefaultHostnameVerifier(new SSLView().hv);
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setTo126NextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setTo126NextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setTo126NextOrder:找到多个支付商商户");
            return null;
        }

        DecimalFormat fnum = new DecimalFormat("##0.00");
        String dd = fnum.format(channelOrder.getAmount());
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/126";
        String signTemp = "amount=" + dd + "&appid=" + businessRate.getPayerBusinessId() + "&callback_url="
                + callbackUrl + "&error_url=" + callbackUrl + "&out_trade_no=" + channelOrder.getPtOrderno()
                + "&out_uid=" + channelOrder.getPtOrderno() + "&pay_type=" + businessRate.getPayerPayType() + "&success_url=" + callbackUrl + "&version=v1.1";
        try {
            log.info("signTemp:" + signTemp + "&key=" + payerBusinessInfos.get(0).getApiKey());
            String sign = Md5Utils.androidMD5(signTemp + "&key=" + payerBusinessInfos.get(0).getApiKey()).toUpperCase();
            signTemp = signTemp + "&sign=" + sign;
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(signTemp, headers);
            RestTemplate restTemplate = new RestTemplate();
            String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
            log.info("setTo126NextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            int code = object.getIntValue("code");
            if (code != 200) {
                log.error("创建126订单失败");
                return object;
            }
            String _payUrl = object.getString("url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 七星
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToQXNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setTo126NextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToQXNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToQXNextOrder:找到多个支付商商户");
            return null;
        }

        try {
            String mer_no = "000115000124"; //商户编码（平台分配）
            String mer_user_no = "000115000124000000000001"; //商户用户编码（平台分配）
            String trans_id = channelOrder.getPtOrderno(); //下游交易编码(必须保证唯一性)
            String trade_type = "T0"; //交易类型(T0或T1)
            String body = "tttt"; //交易或商品的描述
            String version = "1.0.0"; //版本号
            String sign_type = "RSA";
            String callbackUrl = payConfig.getPayCenterHost() + "/callback/qx";
            String front_url = callbackUrl; //回调商户的同步地址
            String notify_url = callbackUrl; //回调商户的异步地址
            String pay_time = String.valueOf(new Date().getTime());//时间戳（当前utc时间的毫秒值）
            String pay_type = businessRate.getPayerPayType();
            String money = channelOrder.getAmount() + ""; //付款金额（单位分,不能有小数点）
            String spbill_create_ip = "";

            //参数加密
            Map<String, String> aesMap = new TreeMap<String, String>();
            aesMap.put("money", money);
            aesMap.put("pay_type", pay_type);
            aesMap.put("trade_type", trade_type);
            String aesJson = StringUtil.toSortPairString(aesMap);
            // epayAesKey
            String data = AESUtil.AESEncrypt(aesJson, payerInfo.getApiKey());

            //参数签名
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("mer_no", mer_no);
            paramMap.put("mer_user_no", mer_user_no);
            paramMap.put("trans_id", trans_id);
            paramMap.put("pay_time", pay_time);
            paramMap.put("notify_url", notify_url);
            paramMap.put("front_url", front_url);
            paramMap.put("body", body);
            paramMap.put("spbill_create_ip", spbill_create_ip);
            paramMap.put("version", version);
            paramMap.put("sign_type", sign_type);
            paramMap.put("data", data);
            String plainText = StringUtil.toSortPairString(paramMap);
            // rsaPrivateKey
            PrivateKey privateKey = CryptoUtil.getRSAPrivateKeyByPriKeyStr(payerInfo.getApiKey(), CryptoUtil.keyAlgorithm);

            String sign = CryptoUtil.digitalSign(plainText, privateKey, CryptoUtil.signAlgorithm);
            //调用在线交易接口
            String urlString = payerInfo.getServiceUrl();
            paramMap.put("sign", sign);
            log.info("FORM表单提交：" + urlString + " " + paramMap.toString());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(plainText + "&sign=" + sign, headers);
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
            log.info("setTo126NextOrder result:" + result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 五福支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToWFNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNextOrder:找到多个支付商商户");
            return null;
        }

        String amount = (channelOrder.getAmount() + "").split("[.]")[0];

        String callbackUrl = payConfig.getPayCenterHost() + "/callback/wf/" + businessRate.getPayerBusinessId();
        String signTempJSON = "{\"institutionID\":\"213793\",\"orderSN\":\"" + channelOrder.getPtOrderno() + "\",\"partnerID\":\"" + payerBusinessInfos.get(0).getBusinessNo() + "\",\"payMode\":\"2\",\"payMoney\":\"" + amount + "\",\"payObjet\":\"1\"" +
                ",\"payType\":\"" + businessRate.getPayerPayType() + "\",\"refreshForResult\":\"" + callbackUrl + "\"}";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("institutionID", "213793");
        jsonObject.put("orderSN", channelOrder.getPtOrderno());
        jsonObject.put("partnerID", payerBusinessInfos.get(0).getBusinessNo());
        jsonObject.put("payMode", "2");
        jsonObject.put("payMoney", amount);
        jsonObject.put("payObjet", "1");
        jsonObject.put("payType", businessRate.getPayerPayType());
        jsonObject.put("refreshForResult", callbackUrl);
        try {
            log.info("signTemp:" + signTempJSON);
            String sign = Md5Utils.androidMD5(signTempJSON + payerBusinessInfos.get(0).getApiKey()).toUpperCase();
            log.info("sign:" + sign);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            jsonObject.put("sign", sign);
            jsonObject.put("signType", "MD");
            HttpEntity<String> entity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
            RestTemplate restTemplate = new RestTemplate();
            String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
            log.info("setToWFNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            int code = resultBody.getIntValue("code");
            if (code != 200) {
                log.error("创建五福订单失败");
                return resultBody;
            }
            String _payUrl = resultBody.getString("paymentUrl");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 顶顶支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToDDNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToDDNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToDDNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToDDNextOrder:找到多个支付商商户");
            return null;
        }
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String amount = (channelOrder.getAmount().multiply(BigDecimal.valueOf(100L)) + "").replace(".00", "").replace(".0", "");
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/dd";
        String stringSignTemp = "appid=ff9c707bcbc540f2856c5d7d03dd4a87&merchantNo=" + payerBusinessInfos.get(0).getBusinessNo() + "&merchantOrderNo=" +
                channelOrder.getPtOrderno() + "&notifyUrl=" + callbackUrl + "&payChannel=" + businessRate.getPayerPayType() + "&payType=01&price=" + amount + "&productName=test&tradeTime=" + time + "&version=1.0";
        String signStr = "ff9c707bcbc540f2856c5d7d03dd4a87" + payerBusinessInfos.get(0).getBusinessNo() +
                channelOrder.getPtOrderno() + callbackUrl + businessRate.getPayerPayType() + "01" + amount + "test" + time + "1.0" + payerBusinessInfos.get(0).getApiKey();
        try {
            String sign = Md5Utils.androidMD5(signStr);
            RestTemplate restTemplate = new RestTemplate();
            stringSignTemp = stringSignTemp + "&sign=" + sign;
            System.out.println("setToDDNextOrder:" + stringSignTemp);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(stringSignTemp, headers);
            String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
            log.info("setToDDNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            String _payUrl = resultBody.getString("payInfo");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 顶顶支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToDXNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToDDNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToDDNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToDDNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/dx";
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String signStr = "chan_code=" + businessRate.getPayerPayType() + "&goods_desc=test&goods_name=test01&mer_order_id=" + channelOrder.getPtOrderno() + "&merno=" + payerBusinessInfos.get(0).getBusinessNo() +
                "&nonce_str=" + System.currentTimeMillis() + "&notify_url=" + callbackUrl + "&return_url=" + returnUrl + "&total_amount=" + channelOrder.getAmount();
        try {
            String sign = Md5Utils.androidMD5(signStr + "&key=" + payerBusinessInfos.get(0).getApiKey()).toUpperCase();
            RestTemplate restTemplate = new RestTemplate();
            signStr = signStr + "&sign=" + sign;
            log.info("setToDXNextOrder:" + signStr);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(signStr, headers);
            String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
            log.info("setToDXNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            String _payUrl = resultBody.getString("pay_info");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 王者支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToWZNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToDDNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToWZNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToWZNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/wz/" + businessRate.getPayerBusinessId();
//        String returnUrl = channelOrder.getReturnUrl();
//        if(returnUrl==null || "".equals(returnUrl)){
//            returnUrl =  payConfig.getPayCenterHost() + "/pay/orderSuccess/"+channelOrder.getOrderId();
//        }
        String amount = channelOrder.getAmount() + "";
        String signStr = "callbackUrl=" + callbackUrl + "&customerRequestedIp=211.97.130.72&merchantNumber=" + businessRate.getPayerBusinessId() + "&merchantOrderNumber=" + channelOrder.getPtOrderno()
                + "&paymentMethod=" + businessRate.getPayerPayType() + "&paymentPlatform=2&requestedAmount=" + amount;
        log.info("setToWZNextOrder sign:" + signStr);
        try {
            String sign = Md5Utils.androidMD5(signStr + "&merchantKey=" + payerBusinessInfos.get(0).getApiKey()).toUpperCase();
            RestTemplate restTemplate = new RestTemplate();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("callbackUrl", callbackUrl);
            jsonObject.put("customerRequestedIp", "211.97.130.72");
            jsonObject.put("merchantNumber", businessRate.getPayerBusinessId());
            jsonObject.put("merchantOrderNumber", channelOrder.getPtOrderno());
            jsonObject.put("paymentMethod", businessRate.getPayerPayType());
            jsonObject.put("paymentPlatform", "2");
            jsonObject.put("requestedAmount", amount);
            jsonObject.put("sign", sign);
            String body = HttpUtils.sendSSLPost("https://merchantgatewayapi.group-pay.biz/api/deposit", jsonObject.toJSONString(), "application/json; charset=utf-8");
            log.info("setToWZNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            JSONObject dataJSON = resultBody.getJSONObject("data");
            String _payUrl = dataJSON.getString("paymentUrl");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 先锋银联
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToXFNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setTo126NextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToXFNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToXFNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/xf";
        String sortParam = "mch_uid=" + businessRate.getPayerBusinessId() + "&" + "notify_url=" + callbackUrl + "&out_trade_no=" + channelOrder.getPtOrderno()
                + "&pay_type=" + businessRate.getPayerPayType() + "&return_url=" + returnUrl + "&timestamp=" + System.currentTimeMillis() + "&total_fee=" + channelOrder.getAmount();
//        String signstr=sortParam+payerBusinessInfos.get(0).getApiKey();
        try {
            log.info("signTemp:" + sortParam + payerBusinessInfos.get(0).getApiKey());
            String sign = Md5Utils.androidMD5(sortParam + payerBusinessInfos.get(0).getApiKey());
            sortParam = sortParam + "&sign=" + sign;
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl() + "?method=pay&" + sortParam;
            log.info("先锋银联：" + url);
            String body = restTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("code");
            if (!code.equals("success")) {
                log.error("创建先锋银联订单失败");
                return object;
            }
            String _payUrl = object.getString("data");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 乘风支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToCFNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToCFNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToCFNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToCFNextOrder:找到多个支付商商户");
            return null;
        }
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String amount = (channelOrder.getAmount().multiply(BigDecimal.valueOf(100L)) + "").replace(".00", "").replace(".0", "");
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/cf";
        String stringSignTemp = "appid=1cab59cd5a0d4daeb625501708dfcaf5&merchantNo=" + payerBusinessInfos.get(0).getBusinessNo() + "&merchantOrderNo=" +
                channelOrder.getPtOrderno() + "&notifyUrl=" + callbackUrl + "&payChannel=" + businessRate.getPayerPayType() + "&payType=01&price=" + amount + "&productName=test&tradeTime=" + time + "&version=1.0";
        String signStr = "1cab59cd5a0d4daeb625501708dfcaf5" + payerBusinessInfos.get(0).getBusinessNo() +
                channelOrder.getPtOrderno() + callbackUrl + businessRate.getPayerPayType() + "01" + amount + "test" + time + "1.0" + payerBusinessInfos.get(0).getApiKey();
        try {
            String sign = Md5Utils.androidMD5(signStr);
            RestTemplate restTemplate = new RestTemplate();
            stringSignTemp = stringSignTemp + "&sign=" + sign;
            System.out.println("setToDDNextOrder:" + stringSignTemp);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(stringSignTemp, headers);
            log.info("setToDDNextOrder url:" + payerInfo.getServiceUrl());
            String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
            log.info("setToDDNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            String _payUrl = resultBody.getString("payInfo");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 真有钱支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToZYQNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToZYQNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToZYQNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToZYQNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/zyq";
        DecimalFormat fnum = new DecimalFormat("##0.00");
        String dd = fnum.format(channelOrder.getAmount());
        String  stringSignTemp ="channel="+businessRate.getPayerPayType()+"&desc=ceshi&ip=192.168.0.0&merId="+businessRate.getPayerBusinessId()+
                "&nonceStr="+System.currentTimeMillis()+"&notifyUrl="+callbackUrl+"&orderAmt="+dd+"&orderId="+channelOrder.getPtOrderno()+"&returnUrl="+returnUrl;
        String sign = "";
        try {
            sign = Md5Utils.androidMD5(stringSignTemp + "&key="+payerBusinessInfos.get(0).getApiKey()).toUpperCase();
            // 再进行
//            String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDFkNwtbDQsf5C0pOadqDrYgO9lKJrmQihBS4cPn/zh9EylEbrvXjoHijrG+t8DNl0rI79+O1TqA477Z8DbHobZecBGm1AtKjp15GvH5tZ7Dq87FQ/JJiEmyKnlqAYxMZxnYrml/X17eMFCP1SKTW9NmMPOGgptWP1+zSM/pdmV2fh56n2HvgWo32bQPqPA/w/fCbuDP/eZ3gWyDyMEi8xfDY1rEUIianJN//NuLudFI2VlJnAhE8/+1Moq9maRWzvrMZdoAqFIL726AZBtUAiB1yGlaTzsuxsCYwC6ToqzE2stFOpbDj+Q6oli92Mw2RLiGbW7PdNr6zvKuO148vxRAgMBAAECggEAPHN/u3JRYJBD4dJXMkRohRimp6hOBUiW8qu+jtxgT28mCJgtP9LwJKB6LapTlLMMvQd6L5DOAbDHDkwH6cQkdOTZHYGmfbtfGfWjuQ378Llkisp+AwlkuW5K9hd8lY7dkZDWx2CSBbbEQ5YHMy5QcUjeGPFE4YKKRkNUlsQTmNMIjVRYoKVzs91mw6m1VE2IpHLZOdXmvYpZGL0GFZ4dWbO9JtzXqNYXhkRU6adcGZgZLfvV6EauIH5oduKIrb+HETWtEwjOAwW4q80M3oz2uBdqoOJzMqgkcoVAub4+LlkEilYUbd3hD9DPrpj7o6Wkpo5zoA3aFoJku3grYpZoxQKBgQDohwldowA3cN6zGIQiV7tnraKB21fMp4OCikCe63quz/kf5JUCLL9IgpqUhqqp0HLEtgy2IDtdNXpGPbWwLMnJxfXB+1S35a7yX1BSVYarqSIiHpsXAkmjiBdFe4cepQBJJsjqrUaLtcKAOlevraoE3Y7Q8JDvrT7cI6tNoKl3CwKBgQDZgljO11lEewLTcNhAM5JPYQPH6dEn4I5v3V2Z+9Rs7bOV5aT64tDvCj7kJSKomlW5AKNIj8snVhS8ac/0V+1QR7fqz8JWQcLjg5K8US92i0qOsgFGi3VluWpkgw2I7K0Hz6ZVzNLPXv6Gz5WGCAMfYI9O+Wf9IdJwdF9ujsuDkwKBgQCzz5Cvhxfox3B/QCsVv5AdCYmML4kCqlso/C4idmglfI/iWMndp6q3sL9GsaiRrq7BbSIBSIV/e5Y1cQzp0oisblewDOGjX4rcxFYBhT+hySWrCs3ViiBVxCxyv6ZqTSAecU6XIy1VZlWlAPjD0mCSaJ5C9nSt0uO0KiUS33WMpwKBgFiHFwJI4AphTsskWL/4vYoVYGaTog9q9E5PePNdbh5PuRI/f6ClhIhmUSamfq7E8jL0MYEtFXCTNqlhH8TBrNNJ/W9S1DQrW9kiCi0flgPXuabOiHpf9I5MoyL5vB6Ixfwx5e9yCDNi5JGWF7OPu0WgFXP9SvfaNj6S/uyOrgKFAoGBALY0ZbUhgAkstV/4CkjmOx9UNOKof9GcEsDwJavF2tph3tipOTwSsxqFrHzJpOH/860Uz9AzdUoRLmjmOIUTSJZ6CEoir3/78r7MTei3Qn7zCNsbl1RJBW3FfL/I0LoYPNVLDybjZPEBAUhZ1bOyT04hhFfSZCBSIMPd5xCMr4UR";
//            sign = SHA256WithRSAUtils.buildRSASignByPrivateKey(md5Sign, privateKey);
//            boolean b = SHA256WithRSAUtils.buildRSAverifyByPublicKey(md5Sign, publicKey,sign);
            RestTemplate restTemplate = new RestTemplate();
            stringSignTemp = stringSignTemp + "&sign=" + sign;
            System.out.println("setToZYQNextOrder:" + stringSignTemp);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(stringSignTemp, headers);
//        String body = HttpUtils.sendPost2("http://api.p5ckq.cn/pay",stringSignTemp);
            String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
            log.info("setToZYQNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("code");
            if (!code.equals("1")) {
                log.error("创建真有钱订单失败");
                return object;
            }
            JSONObject _payUrl = object.getJSONObject("data");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl.getString("payurl"));
            payData.put("third", "true");
            result.put("data", payData);

            return result;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }

//        String sortParam = "mch_uid=" + businessRate.getPayerBusinessId() + "&" + "notify_url=" + callbackUrl + "&out_trade_no=" + channelOrder.getPtOrderno()
//                + "&pay_type=" + businessRate.getPayerPayType() + "&return_url=" + returnUrl + "&timestamp=" + System.currentTimeMillis() + "&total_fee=" + channelOrder.getAmount();
////        String signstr=sortParam+payerBusinessInfos.get(0).getApiKey();
//        try {
//            log.info("signTemp:" + sortParam + payerBusinessInfos.get(0).getApiKey());
//            String sign = Md5Utils.androidMD5(sortParam + payerBusinessInfos.get(0).getApiKey());
//            sortParam = sortParam + "&sign=" + sign;
//            HttpHeaders headers = new HttpHeaders();
//            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//            HttpEntity entity = new HttpEntity(headers);
//            RestTemplate restTemplate = new RestTemplate();
//            String url = payerInfo.getServiceUrl() + "?method=pay&" + sortParam;
//            log.info("先锋银联：" + url);
//            String body = restTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody();
//            log.info("setToXFNextOrder result:" + body);
//            JSONObject object = JSONObject.parseObject(body);
//            String code = object.getString("code");
//            if (!code.equals("success")) {
//                log.error("创建先锋银联订单失败");
//                return object;
//            }
//            String _payUrl = object.getString("data");
//            JSONObject result = new JSONObject();
//            JSONObject payData = new JSONObject();
//            payData.put("payUrl", _payUrl);
//            result.put("data", payData);
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return null;
    }

    /**
     * 重阳支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToChongYangNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToChongYangNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToChongYangNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToChongYangNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/chongyang";
        try {
            String sortParam = "appid=" + businessRate.getPayerBusinessId() + "&appkey=" + payerBusinessInfos.get(0).getApiKey()
                    + "&goodsname=goodsname&membername=membername&money=" +
                    channelOrder.getAmount() + "&notifyurl=" + URLEncoder.encode(callbackUrl, "UTF-8")
                    + "&orderid=" + channelOrder.getPtOrderno() + "&orderperiod=5&paycode=" + businessRate.getPayerPayType() + "&remark=remark&returnurl=" + URLEncoder.encode(returnUrl, "UTF-8") + "&timestamp=" + System.currentTimeMillis();
            String sign = Md5Utils.androidMD5(sortParam);
            log.info("setToXFNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign+"&sign_type=MD5";
            log.info("setToXFNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("state");
            if (!code.equals("true")) {
                log.error("创建重阳支付订单失败");
                return object;
            }
            String _payUrl = object.getString("url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 牛牛支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToNiuNiuNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNiuNiuNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNiuNiuNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNiuNiuNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/niuniu";
        try {
            HashMap<String,String> hashMap = new HashMap<>();
            hashMap.put("return_type","app");
            hashMap.put("appid",businessRate.getPayerBusinessId());
            hashMap.put("pay_type",businessRate.getPayerPayType());
            DecimalFormat fnum = new DecimalFormat("##0.00");
            String dd = fnum.format(channelOrder.getAmount());
            hashMap.put("amount",dd);
            hashMap.put("callback_url",callbackUrl);
            hashMap.put("success_url",returnUrl);
            hashMap.put("error_url",returnUrl);
            hashMap.put("out_uid","123");
            hashMap.put("out_trade_no",channelOrder.getPtOrderno());
            hashMap.put("version","v1.1");
            String sortParam = Util.sortKey(hashMap);
            String sign = Md5Utils.androidMD5(sortParam+"&key="+payerBusinessInfos.get(0).getApiKey());
            log.info("setToXFNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign.toUpperCase();
            log.info("setToXFNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("code");
            if (!code.equals("200")) {
                log.error("创建牛牛支付订单失败");
                return object;
            }
            String _payUrl = object.getString("url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 新牛牛支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToNewNiuNiuNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNewNiuNiuNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNiuNiuNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNiuNiuNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/xinniuniu";
        try {

            String sortParam = "amount="+channelOrder.getAmount()+"&bankCode="+businessRate.getPayerPayType()+"&clientIp="+ ServletUtils.getRequest().getRemoteHost()
                    +"&extend=ttt&goodsDetail=ttt&goodsName=ttt&mchId="+businessRate.getPayerBusinessId()
                    +"&notiryUrl="+callbackUrl+"&orderNo="+channelOrder.getPtOrderno()+"&payTime="+System.currentTimeMillis()+"&returnUrl="+returnUrl+"&signType=MD5";
            String sign = Md5Utils.androidMD5(sortParam+payerBusinessInfos.get(0).getApiKey());
            log.info("setToXFNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign.toUpperCase();
            log.info("setToXFNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("RState");
            if (!code.equals("1")) {
                log.error("创建新牛牛支付订单失败");
                return object;
            }
            String _payUrl = object.getString("Data");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 鑫和支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToXHNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToXHNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToXHNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToXHNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String amount = (channelOrder.getAmount().multiply(BigDecimal.valueOf(100L)) + "").replace(".00", "").replace(".0", "");
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/xinhe";
        String stringSignTemp = "ServerUrl="+callbackUrl+"&amount="+amount+"&backurl="+returnUrl+"&businessnumber="+channelOrder.getPtOrderno()+"&failUrl="+returnUrl+"&goodsName=test&mer_id="+payerBusinessInfos.get(0).getBusinessNo()
                +"&terminal="+businessRate.getPayerPayType()+"&timestamp="+time+"&version=01";
//        String stringSignTemp = "appid=ff9c707bcbc540f2856c5d7d03dd4a87&merchantNo=" + payerBusinessInfos.get(0).getBusinessNo() + "&merchantOrderNo=" +
//                channelOrder.getPtOrderno() + "&notifyUrl=" + callbackUrl + "&payChannel=" + businessRate.getPayerPayType() + "&payType=01&price=" + amount + "&productName=test&tradeTime=" + time + "&version=1.0";
//        String signStr = "ff9c707bcbc540f2856c5d7d03dd4a87" + payerBusinessInfos.get(0).getBusinessNo() +
//                channelOrder.getPtOrderno() + callbackUrl + businessRate.getPayerPayType() + "01" + amount + "test" + time + "1.0" + payerBusinessInfos.get(0).getApiKey();
        try {
            String sign = Md5Utils.androidMD5(stringSignTemp+"&"+ payerBusinessInfos.get(0).getApiKey()).toUpperCase();
            RestTemplate restTemplate = new RestTemplate();
            stringSignTemp = stringSignTemp + "&sign_type=md5&sign=" + sign;
            System.out.println("setToXHNextOrder:" + stringSignTemp);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(stringSignTemp, headers);
            String body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXHNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            JSONObject dataObject = resultBody.getJSONObject("data");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", dataObject.getString("trade_qrcode"));
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 乐购乐付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToLeGouNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNiuNiuNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNiuNiuNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNiuNiuNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/legou";
        try {
            HashMap<String,String> hashMap = new HashMap<>();
            hashMap.put("return_type","app");
            hashMap.put("appid",businessRate.getPayerBusinessId());
            hashMap.put("pay_type",businessRate.getPayerPayType());
            DecimalFormat fnum = new DecimalFormat("##0.00");
            String dd = fnum.format(channelOrder.getAmount());
            hashMap.put("amount",dd);
            hashMap.put("callback_url",callbackUrl);
            hashMap.put("success_url",returnUrl);
            hashMap.put("error_url",returnUrl);
            hashMap.put("out_uid","123");
            hashMap.put("out_trade_no",channelOrder.getPtOrderno());
            hashMap.put("version","v1.1");
            String sortParam = Util.sortKey(hashMap);
            String sign = Md5Utils.androidMD5(sortParam+"&key="+payerBusinessInfos.get(0).getApiKey());
            log.info("setToXFNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign.toUpperCase();
            log.info("setToXFNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("code");
            if (!code.equals("200")) {
                log.error("创建乐购订单失败");
                return object;
            }
            String _payUrl = object.getString("url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 大师支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToDaShiNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToDDNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToWZNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToWZNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/dashi";
        String stringSignTemp =  "fxid="+businessRate.getPayerBusinessId()+"&fxddh="+channelOrder.getPtOrderno()+"&fxfee="
                +channelOrder.getAmount()+"&fxnotifyurl="+callbackUrl;
        String sign = "";
        try {
            sign = Md5Utils.androidMD5(stringSignTemp +"&" + payerBusinessInfos.get(0).getApiKey());
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        stringSignTemp = stringSignTemp + "&fxsign=" + sign+"&fxdesc=ttt&fxbackurl="+returnUrl+"&fxpay="+businessRate.getPayerPayType()
                +"&fxip=127.0.0.1&fxuserid="+channelOrder.getPtOrderno();
        log.info("setToDaShiNextOrder sign:" + stringSignTemp);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(stringSignTemp,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToDaShiNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            String _payUrl = resultBody.getString("payurl");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 帝国支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToDiGuoNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToDiGuoNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToDiGuoNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToDiGuoNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/diguo";
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String stringSignTemp =  "fee="+channelOrder.getAmount()+"&ip="+ServletUtils.getRequest().getRemoteHost()+"&mch_id="
                +businessRate.getPayerBusinessId()+"&method=post&notify_url="+callbackUrl+"&pay_type="+
                businessRate.getPayerPayType()+"&return_rul="+returnUrl+"&trade_id="+channelOrder.getPtOrderno();
//        String stringSignTemp =  "fxid="+businessRate.getPayerBusinessId()+"&fxddh="+channelOrder.getPtOrderno()+"&fxfee="
//                +channelOrder.getAmount()+"&fxnotifyurl="+callbackUrl;
        String sign = "";
        try {
            sign = Md5Utils.androidMD5(payerBusinessInfos.get(0).getApiKey()+ stringSignTemp  + payerBusinessInfos.get(0).getApiKey());
            stringSignTemp = stringSignTemp +"&sign="+sign.toUpperCase();
            log.info("setToDiGuoNextOrder sign:" +stringSignTemp);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(stringSignTemp,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToDaShiNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            JSONObject _resultBody = resultBody.getJSONObject("data");
            String _payUrl = _resultBody.getString("pay_url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 聚核支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToJuHeNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNewNiuNiuNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNiuNiuNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNiuNiuNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/juhe";
        try {
            String sortParam = "child_type=JUHE&mch_id="+businessRate.getPayerBusinessId()+"&mch_secret="+payerBusinessInfos.get(0).getApiKey()+
                    "&notify_url="+URLEncoder.encode(callbackUrl,"UTF-8")
                    +"&out_trade_no="+channelOrder.getPtOrderno()+"&pay_type="+businessRate.getPayerPayType()
                    +"&timestamp="+System.currentTimeMillis()+"&total_fee="+channelOrder.getAmount();
//            String sortParam = "amount="+channelOrder.getAmount()+"&bankCode="+businessRate.getPayerPayType()+"&clientIp="+ ServletUtils.getRequest().getRemoteHost()
//                    +"&extend=ttt&goodsDetail=ttt&goodsName=ttt&mchId="+businessRate.getPayerBusinessId()
//                    +"&notiryUrl="+callbackUrl+"&orderNo="+channelOrder.getPtOrderno()+"&payTime="+System.currentTimeMillis()+"&returnUrl="+returnUrl+"&signType=MD5";
            String sign = Md5Utils.androidMD5(sortParam);
            log.info("setToJuHeNextOrder sortParam:" + sortParam);
            sortParam = sortParam.replace("&mch_secret="+payerBusinessInfos.get(0).getApiKey(),"") + "&sign=" + sign.toUpperCase();
            log.info("setToJuHeNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
//            String code = object.getString("RState");
//            if (!code.equals("1")) {
//                log.error("创建聚核支付订单失败");
//                return object;
//            }
            JSONObject _object = object.getJSONObject("data");
            String _payUrl = _object.getString("url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 南北支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToNanBeiNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNewNiuNiuNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNanBeiNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNanBeiNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/nanbei";
        try {
            String sortParam="mch_uid="+businessRate.getPayerBusinessId()+"&"+"notify_url="+callbackUrl+"&out_trade_no="+channelOrder.getPtOrderno()
                    +"&pay_type="+businessRate.getPayerPayType()+"&return_url="+returnUrl+"&timestamp="+System.currentTimeMillis()+"&total_fee="+channelOrder.getAmount();
//            String sortParam = "child_type=JUHE&mch_id="+businessRate.getPayerBusinessId()+"&mch_secret="+payerBusinessInfos.get(0).getApiKey()+
//                    "&notify_url="+URLEncoder.encode(callbackUrl,"UTF-8")
//                    +"&out_trade_no="+channelOrder.getPtOrderno()+"&pay_type="+businessRate.getPayerPayType()
//                    +"&timestamp="+System.currentTimeMillis()+"&total_fee="+channelOrder.getAmount();
            String sign = Md5Utils.androidMD5(sortParam+payerBusinessInfos.get(0).getApiKey());
            log.info("setToNanBeiNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign;
            log.info("setToNanBeiNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("code");
            if (!code.equals("success")) {
                log.error("创建南北支付订单失败");
                return object;
            }
            String _payUrl = object.getString("data");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 恭喜发财
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToGXFCNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToGXFCNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToGXFCNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToGXFCNextOrder:找到多个支付商商户");
            return null;
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/GXFC";
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }

        Map<String, String> signMap = new TreeMap<String, String>();
        String nonce_str = UUID.randomUUID().toString().replaceAll("-", "");// 随机字符串
        String time_Stamp = "" + (System.currentTimeMillis() / 1000);// 时间戳
        signMap = new TreeMap<String, String>();
        signMap.put("openid", "e1318c9ebae7c83d640173670930a120");// appId
        signMap.put("mch_secret", businessRate.getPayerBusinessId());// 商户ID
        signMap.put("nonce_str", nonce_str);// 随机字符串
        signMap.put("body", "test");// 商品描述
        signMap.put("out_trade_no", channelOrder.getPtOrderno());// 商户订单号
        signMap.put("total_fee", channelOrder.getAmount()+"");// 标价金额
        signMap.put("notify_url", callbackUrl);// 通知地址
        signMap.put("trade_type", businessRate.getPayerPayType());// 交易类型
        signMap.put("device_info", channelOrder.getPtOrderno());// 设备号
        signMap.put("time_Stamp", time_Stamp);// 时间戳
        SignUtil.getSign(signMap, payerBusinessInfos.get(0).getApiKey());
        String reqJson = JSONObject.toJSONString(signMap);
        try {
            log.info("setToGXFCNextOrder sign:" +reqJson);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            HttpEntity entity = new HttpEntity(JSONObject.toJSONString(signMap),headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToGXFCNextOrder result:" + body);
            JSONObject resultBody = JSONObject.parseObject(body);
            String _payUrl = resultBody.getString("QrCodeUrl");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 荷花支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToHeHuaNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToHeHuaNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToHeHuaNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToHeHuaNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/hehua";
        try {
            HashMap<String,String> hashMap = new HashMap<>();
            hashMap.put("mch_id",businessRate.getPayerBusinessId());
            hashMap.put("mch_trade_no",channelOrder.getPtOrderno());
            hashMap.put("pay_type",businessRate.getPayerPayType());
            String amount = (channelOrder.getAmount().multiply(BigDecimal.valueOf(100L)) + "").replace(".00", "").replace(".0", "");
            hashMap.put("total_fee",amount+"");
            hashMap.put("notify_url",callbackUrl);
            hashMap.put("front_url",returnUrl);
            hashMap.put("nonce_str",System.currentTimeMillis()+"");
            hashMap.put("goods_name","test");
            hashMap.put("goods_desc","test");
            String sortParam = Util.sortKey(hashMap);
            String sign = Md5Utils.androidMD5(sortParam+"&key="+payerBusinessInfos.get(0).getApiKey());
            log.info("setToXFNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign;
            log.info("setToXFNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("荷花支付 result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("status");
            if (!code.equals("SUCCESS")) {
                log.error("创建荷花支付订单失败");
                return object;
            }
            String _payUrl = object.getString("qr_code_url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 国际支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToGuoJiNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToGuoJiNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToGuoJiNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToGuoJiNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/guoji";
        try {

            Map<String, String> map = new HashMap<>();
            String reqTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            map.put("merchNo", businessRate.getPayerBusinessId());
            map.put("orderNo", channelOrder.getPtOrderno());
            map.put("amount", new DecimalFormat("0.00").format(channelOrder.getAmount()));
            map.put("currency", "CNY");
            map.put("outChannel", businessRate.getPayerPayType());
            map.put("returnUrl", returnUrl);
            map.put("notifyUrl", callbackUrl);
            map.put("reqTime", reqTime);
            map.put("attach", businessRate.getPayerBusinessId()+"");
            map.put("userId", String.valueOf(new Random().nextInt(100000000)));
            String jsonObj = JSONObject.toJSONString(map);
            String sign = Md5Utils.androidMD5(jsonObj+payerBusinessInfos.get(0).getApiKey());
            String context = Base64.getEncoder().encodeToString(jsonObj.getBytes());
            JSONObject bodyParam = new JSONObject();
            bodyParam.put("sign",sign);
            bodyParam.put("context",context);
            bodyParam.put("encryptType","MD5");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            HttpEntity entity = new HttpEntity(bodyParam.toJSONString(),headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToGuoJiNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String msg = object.getString("msg");
            if (!msg.equals("success")) {
                log.error("创建国际支付订单失败");
                return object;
            }
            String context_ = object.getString("context");
            String payUrlObj =  new String(Base64.getDecoder().decode(context_));
            JSONObject payObj = JSONObject.parseObject(payUrlObj);
            String _payUrl = payObj.getString("pay_url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 泡泡支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToPaoPaoNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToPaoPaoNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToPaoPaoNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToPaoPaoNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/paopao/"+businessRate.getPayerBusinessId();
        try {
//            String sortParam="mch_uid="+businessRate.getPayerBusinessId()+"&"+"notify_url="+callbackUrl+"&out_trade_no="+channelOrder.getPtOrderno()
//                    +"&pay_type="+businessRate.getPayerPayType()+"&return_url="+returnUrl+"&timestamp="+System.currentTimeMillis()+"&total_fee="+channelOrder.getAmount();
            String sortParam = "amount="+channelOrder.getAmount()+"&dcMethod="+businessRate.getPayerPayType()+
                    "&merCode="+businessRate.getPayerBusinessId()+"&notifyUrl="+callbackUrl+"&orderNo="+channelOrder.getPtOrderno()+"&subject=test";
            String sign = Md5Utils.androidMD5(sortParam+"key="+payerBusinessInfos.get(0).getApiKey());
            log.info("setToPaoPaoNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign;
            log.info("setToPaoPaoNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("code");
            if (!code.equals("200")) {
                log.error("创建泡泡支付订单失败");
                return object;
            }
            JSONObject resultObject = object.getJSONObject("result");
            String _payUrl = resultObject.getString("data");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 神奇支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToShenQiNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNewNiuNiuNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToShenQiNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToShenQiNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/shenqi";
        try {
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            String sortParam = "pay_amount=" + channelOrder.getAmount() + "&pay_applydate=" + time + "&pay_callbackurl=" + returnUrl + "&pay_memberid=" +
                    businessRate.getPayerBusinessId() + "&pay_notifyurl=" + callbackUrl + "&pay_orderid=" + channelOrder.getPtOrderno()+"&pay_productname=test"+"&key="+payerBusinessInfos.get(0).getApiKey() ;
            log.info("setToShenQiNextOrder sortParam:" + sortParam);
            String sign = Md5Utils.androidMD5(sortParam).toUpperCase();
            log.info("setToShenQiNextOrder sign:" + sign);
            RestTemplate restTemplate = new RestTemplate();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pay_amount", channelOrder.getAmount());
            jsonObject.put("pay_applydate", time);
            jsonObject.put("pay_callbackurl", returnUrl);
            jsonObject.put("pay_memberid", businessRate.getPayerBusinessId());
            jsonObject.put("pay_notifyurl", callbackUrl);
            jsonObject.put("pay_orderid", channelOrder.getPtOrderno() );
            jsonObject.put("pay_productname", "test");
            jsonObject.put("pay_md5sign", sign);
            log.info("setToShenQiNextOrder json:" + jsonObject.toJSONString());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            HttpEntity<String> entity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
            JSONObject body = restTemplate.exchange(payerInfo.getServiceUrl(), HttpMethod.POST, entity, JSONObject.class).getBody();
            log.info("setToShenQiNextOrder result:" + body);
            String code = body.getString("code");
            if (!code.equals("SUCCESS")) {
                log.error("创建神奇支付订单失败");
                return body;
            }
            JSONObject dataObj = body.getJSONObject("data");
            String _payUrl = dataObj.getString("qrcode_url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 西游支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToXiYouNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToXiYouNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToXiYouNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToXiYouNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/xiyou";
        try {

            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            String sortParam = "pay_amount=" + channelOrder.getAmount() + "&pay_applydate=" + time +
                    "&pay_bankcode=" + businessRate.getPayerPayType() + "&pay_callbackurl=" +
                    callbackUrl + "&pay_memberid=" + businessRate.getPayerBusinessId() +
                    "&pay_notifyurl=" + callbackUrl + "&pay_orderid=" + channelOrder.getPtOrderno() ;
            log.info("setToXiYouNextOrder 加密字符串:" + sortParam+"&key="+payerBusinessInfos.get(0).getApiKey());
            String sign = Md5Utils.androidMD5(sortParam+"&key="+payerBusinessInfos.get(0).getApiKey());
            log.info("setToXiYouNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&pay_md5sign=" + sign.toUpperCase();
            log.info("setToXiYouNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("status");
            if (!code.equals("200")) {
                log.error("创建西游支付订单失败");
                return object;
            }
            String _payUrl = object.getString("pay_url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 浩然支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToHaoRanNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNewNiuNiuNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNanBeiNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNanBeiNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/haoran";
        try {
            String sortParam="mch_uid="+businessRate.getPayerBusinessId()+"&"+"notify_url="+callbackUrl+"&out_trade_no="+channelOrder.getPtOrderno()
                    +"&pay_type="+businessRate.getPayerPayType()+"&return_url="+returnUrl+"&timestamp="+System.currentTimeMillis()+"&total_fee="+channelOrder.getAmount();
//            String sortParam = "child_type=JUHE&mch_id="+businessRate.getPayerBusinessId()+"&mch_secret="+payerBusinessInfos.get(0).getApiKey()+
//                    "&notify_url="+URLEncoder.encode(callbackUrl,"UTF-8")
//                    +"&out_trade_no="+channelOrder.getPtOrderno()+"&pay_type="+businessRate.getPayerPayType()
//                    +"&timestamp="+System.currentTimeMillis()+"&total_fee="+channelOrder.getAmount();
            String sign = Md5Utils.androidMD5(sortParam+payerBusinessInfos.get(0).getApiKey());
            log.info("setToNanBeiNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign;
            log.info("setToNanBeiNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("code");
            if (!code.equals("success")) {
                log.error("创建南北支付订单失败");
                return object;
            }
            String _payUrl = object.getString("data");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 自由支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToZiYouNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToNewNiuNiuNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToNanBeiNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToNanBeiNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = payConfig.getPayCenterHost() + "/callback/ziyou";
        try {
            String sortParam="mch_uid="+businessRate.getPayerBusinessId()+"&"+"notify_url="+callbackUrl+"&out_trade_no="+channelOrder.getPtOrderno()
                    +"&pay_type="+businessRate.getPayerPayType()+"&return_url="+returnUrl+"&timestamp="+System.currentTimeMillis()+"&total_fee="+channelOrder.getAmount();
//            String sortParam = "child_type=JUHE&mch_id="+businessRate.getPayerBusinessId()+"&mch_secret="+payerBusinessInfos.get(0).getApiKey()+
//                    "&notify_url="+URLEncoder.encode(callbackUrl,"UTF-8")
//                    +"&out_trade_no="+channelOrder.getPtOrderno()+"&pay_type="+businessRate.getPayerPayType()
//                    +"&timestamp="+System.currentTimeMillis()+"&total_fee="+channelOrder.getAmount();
            String sign = Md5Utils.androidMD5(sortParam+payerBusinessInfos.get(0).getApiKey());
            log.info("setToNanBeiNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign;
            log.info("setToNanBeiNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToXFNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("code");
            if (!code.equals("success")) {
                log.error("创建南北支付订单失败");
                return object;
            }
            String _payUrl = object.getString("data");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 全荣支付
     *
     * @param channelOrder
     * @return
     */
    public JSONObject setToMayaNextOrder(ChannelOrder channelOrder, BusinessRate businessRate) {
        // 设置支付商id
        PayerInfo payerInfo = payerInfoService.selectPayerInfoById(businessRate.getPayerId());
        if (payerInfo == null) {
            log.error("setToMayaNextOrder:找不到支付商");
            return null;
        }
        // 设置支付商id
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setPayerId(businessRate.getPayerId());
        search.setBusinessNo(businessRate.getPayerBusinessId());
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        if (payerBusinessInfos.size() == 0) {
            log.error("setToMayaNextOrder:找不到支付商商户");
            return null;
        }
        if (payerBusinessInfos.size() > 1) {
            log.error("setToMayaNextOrder:找到多个支付商商户");
            return null;
        }
        String returnUrl = channelOrder.getReturnUrl();
        if (returnUrl == null || "".equals(returnUrl)) {
            returnUrl = payConfig.getPayCenterHost() + "/pay/orderSuccess/" + channelOrder.getOrderId();
        }
        String callbackUrl = "http://182.61.171.87:9999/callback/maya";
        try {
            long sendTime = System.currentTimeMillis() / 1000;
            String sortParam = "merchant=" + businessRate.getPayerBusinessId() + "&qrtype=" + businessRate.getPayerPayType() + "&customno=" + channelOrder.getPtOrderno() + "&money=" + channelOrder.getAmount()
                    + "&sendtime=" + sendTime
                    + "&notifyurl=" + callbackUrl + "&backurl=" + returnUrl + "&risklevel=";
            String sign = Md5Utils.androidMD5(sortParam+payerBusinessInfos.get(0).getApiKey());
            log.info("setToMayaNextOrder sortParam:" + sortParam);
            sortParam = sortParam + "&sign=" + sign;
            log.info("setToMayaNextOrder post:" + sortParam);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity entity = new HttpEntity(sortParam,headers);
            RestTemplate restTemplate = new RestTemplate();
            String url = payerInfo.getServiceUrl();
            String body = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();
            log.info("setToMayaNextOrder result:" + body);
            JSONObject object = JSONObject.parseObject(body);
            String code = object.getString("errCode");
            if (!code.equals("0")) {
                log.error("创建玛雅支付订单失败");
                return object;
            }
            String _payUrl = object.getString("url");
            JSONObject result = new JSONObject();
            JSONObject payData = new JSONObject();
            payData.put("payUrl", _payUrl);
            payData.put("third", "true");
            result.put("data", payData);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
